package com.nit.lab.service.system.impl;

import com.nit.lab.dto.basic.TeacherDTO;
import com.nit.lab.dto.system.SchoolDTO;
import com.nit.lab.dto.utils.PagingDTO;
import com.nit.lab.dto.utils.TableReqDTO;
import com.nit.lab.dto.utils.TableRspDTO;
import com.nit.lab.entity.system.SchoolEntity;
import com.nit.lab.mapper.system.SchoolMapper;
import com.nit.lab.service.system.SchoolService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
public class SchoolServiceImpl implements SchoolService {

    @Autowired(required = false)
    private SchoolMapper schoolMapper;

    @Override
    public TableRspDTO list4Table(TableReqDTO tableReqDTO) throws Exception {
        Long count = schoolMapper.count4Table(
                tableReqDTO.parseQueryParam("schoolCode"),
                tableReqDTO.parseQueryParam("schoolName"));
        PagingDTO pagingDTO = new PagingDTO(
                tableReqDTO.getPageNo(),
                tableReqDTO.getPageSize(),
                count,
                schoolMapper.list4Table(
                        tableReqDTO.getStart(),
                        tableReqDTO.getPageSize(),
                        tableReqDTO.parseQueryParam("schoolCode"),
                        tableReqDTO.parseQueryParam("schoolName"),
                        tableReqDTO.getSortField(),
                        tableReqDTO.getSortOrder()));
        TableRspDTO tableRspDTO = new TableRspDTO(pagingDTO);
        return tableRspDTO;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer add(SchoolDTO schoolDTO) throws Exception {
        SchoolEntity schoolEntity = new SchoolEntity();
        schoolEntity.setSchoolCode(schoolDTO.getSchoolCode());
        schoolEntity.setSchoolName(schoolDTO.getSchoolName());
        schoolEntity.setSchoolGrade(schoolDTO.getSchoolGrade());
        schoolEntity.setSchoolInfo(schoolDTO.getSchoolInfo());
        schoolEntity.setGmtCreate(new Date());
        return schoolMapper.add(schoolEntity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer update(SchoolDTO schoolDTO) throws Exception {
        SchoolEntity schoolEntity = new SchoolEntity();
        schoolEntity.setSchoolId(schoolDTO.getSchoolId());
        schoolEntity.setSchoolCode(schoolDTO.getSchoolCode());
        schoolEntity.setSchoolName(schoolDTO.getSchoolName());
        schoolEntity.setSchoolGrade(schoolDTO.getSchoolGrade());
        schoolEntity.setSchoolInfo(schoolDTO.getSchoolInfo());
        return schoolMapper.update(schoolEntity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer remove(String schoolIds) throws Exception {
        String[] arraySchoolId = schoolIds.split(",");
        for (int i = 0; i < arraySchoolId.length; i++) {
            schoolMapper.remove(Long.parseLong(arraySchoolId[i]));
        }
        return arraySchoolId.length;
    }

    @Override
    public List<SchoolDTO> listSchool() throws Exception{
        return schoolMapper.listSchool();
    }
}
